package com.fau.fm.action.index;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.fm.action.PublicAction;
import com.fau.fm.entity.MedicalAdmin;
import com.fau.fm.entity.MedicalOrderReconClaim;
import com.fau.fm.util.DateUtil;
import com.fau.fm.util.FileNameEncoderUtil;
import com.fau.fm.util.SystemConfig;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 我的回款信息Action
 * @author dangjh
 * @datetime 2014年11月21日,下午11:36:03
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CheckingMyOrderReconAction extends PublicAction implements ModelDriven<MedicalOrderReconClaim> {

	private static final long serialVersionUID = -5790531235930861274L;

	private MedicalOrderReconClaim bean = new MedicalOrderReconClaim();
	
	/**
	 * @desc 认领订单
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月21日,下午11:39:38
	 */
	@Action(value = "my_claim_order_recon")
	public String claimOrderRecon() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		boolean flag = this.baseAllService.getCheckingService().saveRecon(bean, admin);
		if(flag) {
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单回款
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月21日,下午11:39:38
	 */
	@Action(value = "my_claim_order_checking_recon")
	public String claimOrderChecking() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		bean.setHospitalId(admin.getEnterpriseId());
		boolean flag = this.baseAllService.getCheckingService().updateRecon(bean);
		if(flag) {
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 待回款-订单回款
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月21日,下午11:39:38
	 */
	@Action(value = "wait_order_checking_recon")
	public String waitOrderChecking() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		boolean flag = this.baseAllService.getCheckingService().saveWaitRecon(bean, admin);
		if(flag) {
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 进入我的待回款订单页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午4:15:34
	 */
	@Action(value = "my_pending_recon_init", results = {@Result(location = "my_pending_recon_init.jsp")})
	public String myPendingRecon(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入我的线下汇款页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午4:15:34
	 */
	@Action(value = "my_line_remittance_recon_init", results = {@Result(location = "my_line_remittance_recon_init.jsp")})
	public String myLineRemittanceRecon(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入我的货到付款页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午4:15:34
	 */
	@Action(value = "my_on_delivery_recon_init", results = {@Result(location = "my_on_delivery_recon_init.jsp")})
	public String myOnDeliveryRecon(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载待回款页面数据
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月21日,下午4:20:14
	 */
	@Action(value = "my_pending_recon_view")
	public String myPendingReconView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getCheckingService().findAllOrderReconClaim(map, bean, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 进入我的已回款订单页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午4:29:32
	 */
	@Action(value = "my_audited_recon_init", results = {@Result(location = "my_audited_recon_init.jsp")})
	public String myAuditedRecon(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入我的已回款线下汇款订单页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午4:29:32
	 */
	@Action(value = "my_audited_remittance_recon_init", results = {@Result(location = "my_audited_remittance_recon_init.jsp")})
	public String myAuditedRemittanceRecon(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入我的已回款货到付款订单页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午4:29:32
	 */
	@Action(value = "my_audited_delivery_recon_init", results = {@Result(location = "my_audited_delivery_recon_init.jsp")})
	public String myAuditedDeliveryRecon(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载我的已回款数据
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,上午10:16:49
	 */
	@Action(value = "my_audited_recon_view")
	public String myAuditedReconView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getCheckingService().findAllOrderBackSection(map, bean, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 我的已回款订单信息导出
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月24日,下午5:16:01
	 */
	@Action(value = "exports_my_orders_recon")
	public String exports() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"我的已回款订单信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getCheckingService().selectedPage(bean);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出全部我的已回款订单信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午5:17:06
	 */
	@Action(value = "exports_all_my_orders_recon")
	public String exportsAll() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			bean.setClaimUserId(user.getId());
			bean.setHospitalId(user.getEnterpriseId());
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"我的已回款订单信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getCheckingService().allPage(bean);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 查看订单明细
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年12月2日 上午9:36:05
	 */
	@Action(value = "my_order_recon_detail_view", results = {@Result(name = SUCCESS, location = "my_order_recon_detail_view.jsp")})
	public String myOrderDetailView() {
		return SUCCESS;
	}
	
	/**
	 * @desc 批量导入订单为已回款
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月27日 下午5:17:45
	 */
	@Action(value = "my_batch_recon_import", results = {@Result(name = SUCCESS, location = "my_batch_recon_import.jsp")})
	public String batchReconImport() {
		return SUCCESS;
	}
	
	/**
	 * @desc 批量修改订单为已回款
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 下午4:17:41
	 */
	@Action(value = "my_batch_recon_orders")
	public String batchRecon() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request, response);
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String info = "";
		try {
			info = this.baseAllService.getCheckingService().importReconDelivery(admin, file, SystemConfig.MY_WAIT_SECTION_CASH);
		} catch (Exception e) {
			map.put("success", false);
			map.put("message", "请导入");
			formatJSON(response, map);
			return null;
		}
		map.put("success", true);
		map.put("message", info);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量导入订单为已回款(线下)
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月27日 下午5:17:45
	 */
	@Action(value = "my_batch_recon_line_import", results = {@Result(name = SUCCESS, location = "my_batch_recon_line_import.jsp")})
	public String batchReconLineImport() {
		return SUCCESS;
	}
	
	/**
	 * @desc 批量修改订单为已回款
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 下午4:17:41
	 */
	@Action(value = "my_batch_line_recon_orders")
	public String batchLineRecon() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request, response);
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String info = "";
		try {
			info = this.baseAllService.getCheckingService().importLineReconDelivery(admin, file, SystemConfig.MY_WAIT_SECTION_LINE);
		} catch (Exception e) {
			map.put("success", false);
			map.put("message", "请导入");
			formatJSON(response, map);
			return null;
		}
		map.put("success", true);
		map.put("message", info);
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalOrderReconClaim getModel() {
		return bean;
	}

}
